package com.qms.qep.controller;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qms.common.annotation.Log;
import com.qms.common.core.controller.BaseController;
import com.qms.common.core.domain.AjaxResult;
import com.qms.common.core.page.TableDataInfo;
import com.qms.common.enums.BusinessType;
import com.qms.common.utils.poi.ExcelUtil;
import com.qms.qep.domain.InspectionData;
import com.qms.qep.domain.dto.InspectionDataDTO;
import com.qms.qep.service.IInspectionDataService;

/**
 * 检验数据Controller
 * 
 * @author qms
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/qep/inspection/data")
public class InspectionDataController extends BaseController
{
    @Autowired
    private IInspectionDataService inspectionDataService;

    /**
     * 查询检验数据列表
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionData inspectionData)
    {
        startPage();
        List<InspectionData> list = inspectionDataService.selectInspectionDataList(inspectionData);
        return getDataTable(list);
    }

    /**
     * 查询检验数据DTO列表
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:list')")
    @GetMapping("/listDTO")
    public TableDataInfo listDTO(InspectionData inspectionData)
    {
        startPage();
        List<InspectionDataDTO> list = inspectionDataService.selectInspectionDataDTOList(inspectionData);
        return getDataTable(list);
    }

    /**
     * 根据任务ID查询检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:list')")
    @GetMapping("/listByTaskId/{taskId}")
    public AjaxResult listByTaskId(@PathVariable("taskId") Integer taskId)
    {
        List<InspectionData> list = inspectionDataService.selectInspectionDataByTaskId(taskId);
        return success(list);
    }

    /**
     * 根据任务ID查询检验数据DTO
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:list')")
    @GetMapping("/listDTOByTaskId/{taskId}")
    public AjaxResult listDTOByTaskId(@PathVariable("taskId") Integer taskId)
    {
        List<InspectionDataDTO> list = inspectionDataService.selectInspectionDataDTOByTaskId(taskId);
        return success(list);
    }

    /**
     * 导出检验数据列表
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:export')")
    @Log(title = "检验数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionData inspectionData)
    {
        List<InspectionData> list = inspectionDataService.selectInspectionDataList(inspectionData);
        ExcelUtil<InspectionData> util = new ExcelUtil<InspectionData>(InspectionData.class);
        util.exportExcel(response, list, "检验数据数据");
    }

    /**
     * 导出选中的检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:export')")
    @Log(title = "检验数据", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelected")
    public void exportSelected(HttpServletResponse response, @RequestParam String ids)
    {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(id -> Integer.parseInt(id))
                .collect(Collectors.toList());
        List<InspectionData> list = inspectionDataService.selectInspectionDataByIds(idList);
        ExcelUtil<InspectionData> util = new ExcelUtil<InspectionData>(InspectionData.class);
        util.exportExcel(response, list, "选中检验数据");
    }

    /**
     * 导入检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:import')")
    @Log(title = "检验数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Integer taskId, boolean updateSupport) throws Exception
    {
        ExcelUtil<InspectionData> util = new ExcelUtil<InspectionData>(InspectionData.class);
        List<InspectionData> dataList = util.importExcel(file.getInputStream());
        String message = inspectionDataService.importData(dataList, taskId, updateSupport, getUsername());
        return success(message);
    }

    /**
     * 获取检验数据导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<InspectionData> util = new ExcelUtil<InspectionData>(InspectionData.class);
        util.importTemplateExcel(response, "检验数据");
    }

    /**
     * 获取检验数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(inspectionDataService.selectInspectionDataById(id));
    }

    /**
     * 新增检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:add')")
    @Log(title = "检验数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionData inspectionData)
    {
        return toAjax(inspectionDataService.insertInspectionData(inspectionData));
    }

    /**
     * 批量新增检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:add')")
    @Log(title = "检验数据", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<InspectionData> inspectionDataList)
    {
        return toAjax(inspectionDataService.batchInsertInspectionData(inspectionDataList));
    }

    /**
     * 修改检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:edit')")
    @Log(title = "检验数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionData inspectionData)
    {
        return toAjax(inspectionDataService.updateInspectionData(inspectionData));
    }

    /**
     * 删除检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:remove')")
    @Log(title = "检验数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(inspectionDataService.deleteInspectionDataByIds(ids));
    }

    /**
     * 根据任务ID删除检验数据
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:data:remove')")
    @Log(title = "检验数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/task/{taskId}")
    public AjaxResult removeByTaskId(@PathVariable("taskId") Integer taskId)
    {
        return toAjax(inspectionDataService.deleteInspectionDataByTaskId(taskId));
    }

    /**
     * 根据任务ID获取检验参数列表及其标准值
     */
    @GetMapping("/parameters/{taskId}")
    public AjaxResult getParametersByTaskId(@PathVariable("taskId") Long taskId)
    {
        List<Map<String, Object>> parameterList = inspectionDataService.getParametersByTaskId(taskId);
        return success(parameterList);
    }
} 